//
//  SmallDecisionsSjsViewController.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/7/25.
//

#import "SmallDecisionsSjsViewController.h"
#import "SmallDecisionsSjsCell.h"

@interface SmallDecisionsSjsViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *sjsTableView;
@property (nonatomic, strong) NSArray *sjsList;
@property (nonatomic, strong) UIButton *refreshBtn;

@end

@implementation SmallDecisionsSjsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"随机数";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.sjsList = [self generateRandomNumbersWithMin:self.min max:self.max count:self.count allowDuplicates:self.allowDuplicates];
    [self sjsTableView];
    [self refreshBtn];
}

- (NSArray *)generateRandomNumbersWithMin:(NSInteger)min
                                     max:(NSInteger)max
                                   count:(NSInteger)count
                           allowDuplicates:(BOOL)allowDuplicates {


    NSMutableArray *result = [NSMutableArray array];

    if (allowDuplicates) {
        // 允许重复的情况
        for (NSInteger i = 0; i < count; i++) {
            NSInteger randomNum = [self randomNumberBetweenMin:min andMax:max];
            [result addObject:@(randomNum)];
        }
    } else {
        // 不允许重复的情况
        NSMutableSet *uniqueNumbers = [NSMutableSet set];
        while (uniqueNumbers.count < count) {
            NSInteger randomNum = [self randomNumberBetweenMin:min andMax:max];
            [uniqueNumbers addObject:@(randomNum)];
        }
        [result addObjectsFromArray:[uniqueNumbers allObjects]];
    }

    return [result copy];
}

// 生成指定范围内的随机数
- (NSInteger)randomNumberBetweenMin:(NSInteger)min andMax:(NSInteger)max {
    // arc4random_uniform 返回 0 到 upper_bound-1 的随机数
    // 所以我们需要调整范围
    return min + arc4random_uniform((uint32_t)(max - min + 1));
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.sjsList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SmallDecisionsSjsCell *sjsCell = [tableView dequeueReusableCellWithIdentifier:@"SmallDecisionsSjsCellId" forIndexPath:indexPath];
    sjsCell.contentLabel.text = [NSString stringWithFormat:@"%@",self.sjsList[indexPath.row]];
    return sjsCell;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return  [[UIView alloc] init];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return  0.0001f;
}

- (UITableView *)sjsTableView {
    if (!_sjsTableView) {
        _sjsTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _sjsTableView.delegate   = self;
        _sjsTableView.dataSource = self;
        _sjsTableView.backgroundColor = [UIColor clearColor];
        _sjsTableView.rowHeight = 80;
        [self.view addSubview:_sjsTableView];
        _sjsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_sjsTableView registerClass:[SmallDecisionsSjsCell class] forCellReuseIdentifier:@"SmallDecisionsSjsCellId"];
        [_sjsTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.mas_equalTo(0);
            make.bottom.equalTo(self.refreshBtn.mas_top).offset(-20);
        }];
    }
    return _sjsTableView;
}


- (UIButton *)refreshBtn {
    if(!_refreshBtn) {
        _refreshBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_refreshBtn setTitle:@"刷新数据" forState:UIControlStateNormal];
        [_refreshBtn setTitleColor:SmallDecisionsThemeFontColor forState:UIControlStateNormal];
        [_refreshBtn setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted];
        _refreshBtn.titleLabel.font = SmallDecisionsSetBoldFontSize(14);
        _refreshBtn.layer.cornerRadius = 25;
        _refreshBtn.layer.masksToBounds = YES;
        _refreshBtn.backgroundColor = SmallDecisionsThemeColor;
        [_refreshBtn addTarget:self action:@selector(refreshBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_refreshBtn];
        [_refreshBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(50);
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
            make.bottom.mas_equalTo(-self.view.safeAreaInsets.bottom).offset(-30);
        }];
      }
    return _refreshBtn;
}

- (void)refreshBtnAction {
    self.sjsList = [self generateRandomNumbersWithMin:self.min max:self.max count:self.count allowDuplicates:self.allowDuplicates];
    [self.sjsTableView reloadData];
}
@end
